package com.movitech.mobile.cz.base.common.util;

import java.util.Hashtable;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ADUtil {
	  static Log logger = LogFactory.getLog(ADUtil.class);
	  private  LdapContext ctx = null;
	  private static Properties pro = null;
	  //private final InputStream inStream;
	  private static ADUtil INSTANCE;

	 /* private ADUtil() {
			//inStream = this.getClass().getResourceAsStream("/adconfig.properties");
			pro = new Properties();
			try {
				//pro.load(inStream);
			} catch (Exception e) {
				e.printStackTrace();
			} finally{
				try{
					//inStream.close();
				} catch(Exception e){
					e.printStackTrace();
				}
			}
	  }*/

	 /*
	 *连接AD服务器，
	 * @param adBean
	 * @param returnedAtts
	 * @return
	 */
	public  boolean checkAdUser(String username,String password) {
		Hashtable<String, String> env = new Hashtable<String, String>();
		env.put(Context.SECURITY_AUTHENTICATION, "simple");
		env.put(Context.SECURITY_PRINCIPAL,username + "@" + pro.getProperty("domain"));// User
		env.put(Context.SECURITY_CREDENTIALS, password);// Password
		env.put(Context.INITIAL_CONTEXT_FACTORY,
				"com.sun.jndi.ldap.LdapCtxFactory");
		env.put(Context.PROVIDER_URL, "ldap://" + pro.getProperty("host") + ":"+ pro.getProperty("port"));
		try {
			ctx = new InitialLdapContext(env, null);
			return true;
		} catch (Exception e) {
			logger.error("ADUtil 连接AD服务器失败："+"%%"+username+"%%",e);
			return false;
		} finally{
			try{
				if(ctx != null)
					ctx.close();
			} catch(Exception e){
				e.printStackTrace();
			}
		}
		
	}
	 /*
		 *连接AD服务器，
		 * @param adBean
		 * @param returnedAtts
		 * @return
		 */
		public  boolean checkAdUser2(String username,String password) {
			Hashtable<String, String> env = new Hashtable<String, String>();
			env.put(Context.SECURITY_AUTHENTICATION, "simple");
			env.put(Context.SECURITY_PRINCIPAL,username + "@" + "corp.numessage.net");// User
			env.put(Context.SECURITY_CREDENTIALS, password);// Password
			env.put(Context.INITIAL_CONTEXT_FACTORY,
					"com.sun.jndi.ldap.LdapCtxFactory");
			env.put(Context.PROVIDER_URL, "ldap://" + "taeda.corp.numessage.net" + ":"+ "389");
			try {
				ctx = new InitialLdapContext(env, null);
				return true;
			} catch (Exception e) {
				logger.error("ADUtil 连接AD服务器失败："+"%%"+username+"%%",e);
				e.printStackTrace();
				return false;
			} finally{
				try{
					if(ctx != null)
						ctx.close();
				} catch(Exception e){
					e.printStackTrace();
				}
			}
			
		}

	public static ADUtil getInstace(){
		if (INSTANCE == null) {
			synchronized (ADUtil.class) {
				if (INSTANCE == null) {
					try {
						INSTANCE = new ADUtil();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
		return INSTANCE;
	}
	public static void main(String [] arg){
		ADUtil ad =new ADUtil();
		System.out.println(ad.checkAdUser2("Aibl.He","shuidiguo123Q"));
	}

}
